package com.vint.leetcode;

import com.vint.common.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/*
 * 1. done
 * 2. easy
 * 3. tree,
 * 二叉树层次遍历
 */
public class Leetcode102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
	    List<List<Integer>> ret = new ArrayList<>();
	    if(null==root)return ret;
	    Queue<TreeNode> queue = new LinkedList<>();
	    queue.add(root);
	    while(!queue.isEmpty()){
	        List<Integer> level = new ArrayList<>();
	        int len = queue.size();
	        for(int i=0; i<len; i++) {
	        	TreeNode treeNode = queue.peek();
	        	level.add(queue.poll().val);
	        	if(treeNode.left!=null) {
	        		queue.add(treeNode.left);
		        }
		        if(treeNode.right!=null){
	        		queue.add(treeNode.right);
		        }
	        }
	        ret.add(level);
	    }
	    return ret;
    }
  }
